﻿using Dapper;
using HR.Config;
using HR.IService;
using HR.Models;
using System.Data.SqlClient;

namespace HR.Service
{
    public class SalaryGrantService : ISalaryGrantService
    {
        private readonly HRDBContext db;
        private readonly IConfiguration icon;
        private readonly string? constr;

        public SalaryGrantService(HRDBContext db, IConfiguration icon)
        {
            this.db = db;
            this.icon = icon;
            this.constr = this.icon["ConnectionStrings"];
        }

        public Task<List<salary_grant>> SalaryGrantFindWay(int flagid)
        {
            return Task.Run(() =>
            {
                List<salary_grant> list = new List<salary_grant>();
                //查询一级的
                if (flagid == 1)
                {
                    list = db.salary_grants.Where(sg => sg.second_kind_id=="" && sg.third_kind_id == "").ToList();
                }//查询二级的
                else if (flagid == 2)
                {
                    list = db.salary_grants.Where(sg => sg.second_kind_id != "" && sg.third_kind_id == "").ToList();
                }//查询三级的
                else
                {
                    list = db.salary_grants.Where(sg => sg.third_kind_id != "").ToList();
                }
                return list;
            });
        }

        public Task<Paging<salary_grant>> SalaryGrantPGFills(int pgsize, int pgcurrent)
        {
            return Task.Run(() =>
            {
                //拿到未作复核的薪酬发放信息
                List<salary_grant> list = db.salary_grants.Where(sg => sg.check_status == 0).ToList();
                List<salary_grant> tabs = list.Skip((pgcurrent - 1) * pgsize).Take(pgsize).ToList();
                Paging<salary_grant> pg = new Paging<salary_grant>()
                {
                    rows = list.Count,
                    tabs = tabs
                };
                return pg;
            });
        }
        public Task<int> SalaryGrantCheck(salary_grant_Check sgc, List<salary_grant_detail> list)
        {
            return Task.Run(() =>
            {
                salary_grant? sg = db.salary_grants.Find(sgc.sgr_id);
                sg.salary_standard_sum = sgc.salary_standard_sum;
                sg.salary_paid_sum = sgc.salary_paid_sum;
                sg.checker = sgc.checker;
                sg.check_time = sgc.check_time;
                sg.check_status = 1;
                //遍历对每个薪酬明细的值修改
                foreach (salary_grant_detail item in list)
                {
                    salary_grant_detail? sgd = db.salary_grant_details.Find(item.grd_id);
                    sgd.bouns_sum = item.bouns_sum;
                    sgd.sale_sum = item.sale_sum;
                    sgd.deduct_sum = item.deduct_sum;
                    sgd.salary_standard_sum = item.salary_standard_sum;
                    sgd.salary_paid_sum = item.salary_paid_sum;
                    //修改人力资源档案的发放工资状态为已发放  True，每个薪酬明细对应一个人力资源拿档案
                    human_file? hf = db.human_files.Where(hm => hm.human_id == item.human_id).FirstOrDefault();
                    hf.salary_grant_status = true;
                }
                return db.SaveChanges();
            });
        }

        public Task<Paging<salary_grant>> SalaryGrantFind(SalaryStandardDimPage ssdg)
        {
            return Task.Run(() =>
            {
                Paging<salary_grant> pg = new Paging<salary_grant>();
                using (SqlConnection con = new SqlConnection(constr))
                {
                    string sql = $@"select * from [dbo].[salary_grant] where check_status=1 and salary_grant_id like '%{ssdg.id}%' and
                                (salary_standard_id like '%{ssdg.key_word}%' or salary_grant_id like '%{ssdg.key_word}%')";
                    //这里要判断是否有选择了日期阶段，日期模糊区间查询字段不能为空，上面的模糊查询是字段，可以为空
                    if (ssdg.open_time != null || ssdg.close_time != null)
                    {
                        sql += $" and (check_time between '{ssdg.open_time}' and '{ssdg.close_time}')";
                    }
                    List<salary_grant> list = con.Query<salary_grant>(sql).ToList();
                    List<salary_grant> tabs = list.Skip((ssdg.pgcurrent - 1) * ssdg.pgsize).Take(ssdg.pgsize).ToList();
                    pg.tabs = tabs;
                    pg.rows = list.Count;
                }
                return pg;
            });
        }
    }
}
